
package com.product.android.commonInterface.album;

import org.json.JSONObject;

import com.common.android.utils.JSONUtils;
import com.common.android.utils.MD5ArithmeticUtils;
import com.common.android.utils.StringUtils;
import com.product.android.business.ApplicationVariable;
import com.product.android.business.config.Configuration;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class Image  implements Parcelable{
	public final static String APPCODE = "appcode=" + Configuration.ALBUM_APPCODE;
	//public final static String PERSONALAPITYPE = "apitype=p"; // 个人相册接口
	public final static String UNITAPITYPE = "apitype=u"; // 企业单位相册接口
	public final static String GROUPAPITYPE = "apitype=g"; // 群组相册接口
	
	/**
	 * 图片类型 （0表示非全景图，1表示全景图，其它保留）
	 * 
	 * <br>Created 2014-8-28 下午2:04:20
	 * @version  Image
	 * @author   huangszh		
	 *
	 * @see 	 
	 * 
	 * Copyright(c) 2009-2014, TQ Digital Entertainment, All Rights Reserved
	 *
	 */
	public final class ImageType{
		/** 非全景图片 */
		public final static int NOT_360_PIC = 0;
		/** 全景图片 */
		public final static int IS_360_PIC = 1;
	}
	
	public final static String IMAGE_DOMAIN = "http://img0.pp.99.com"; 
	 
	private long mImageId;  // 图片id
	private String mUrl160;  // 缩略图160 去组合
	private String mUrl480;  // 缩略图480
	private String mFileMd5; // 文件md5
	private int mNavCnt;     //浏览次数  
	private long mGid; 	  // 群相册对应的群id
	private long mCreateUid;  // 创建者id
	private String mFileUrl;   //文件链接地址
	private String mCreateTime; // 创建时间（秒）
	private String mUpdateTime; // 更新时间（秒）
	private int mCommentCnt;   //总评论次数
	private int mPraiseCnt ;    //赞总次数
	private int mIsMyPraise;   //我是否赞过
	private long mAlbumId; // 所属相册id
	private int mImageType; // 0表示非全景图，1表示全景图，其它保留
    
    public Image(){
    	
    }

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Image){
			Image image = (Image)obj;
			if(mFileMd5!=null&&mFileMd5.equals(image.getFile_dna())
					&&mUrl160!=null&&mUrl160.equals(image.getUrl_160()))
				return true;
		}
		return false;
	}
	
	/**
	 * 解析JSON中的图片信息
	 * 
	 * <br>Created 2014-9-3 下午2:27:41
	 * @param json 图片信息
	 * @author       huangszh
	 */
	public void parseImageInfoJson(JSONObject json) {
		
		parseImageInfoJson(json, 0, 0);
	}
 	
	/**
	 * 解析JSON中的图片信息
	 * 
	 * <br>Created 2014-9-3 下午2:26:22
	 * @param json 图片信息
	 * @param uid  创建者uid(传0则直接从JSON中获取，不传0目前用于解析相册中已存在的图片信息)
	 * @param albumId 图片所属专辑id
	 * @author       huangszh
	 */
 	public void parseImageInfoJson(JSONObject json,long uid,long albumId){
		 if(uid != 0){
			 mCreateUid = uid;
		 } else{
			 mCreateUid = json.optLong("user_id");
		 }
		 mImageId = json.optLong("pic_id");
		 mIsMyPraise = json.optInt("liked");
		 mPraiseCnt = json.optInt("likedcnt");
		 mNavCnt = json.optInt("nav_cnt");
		 mFileMd5 = json.optString("file_dna");
		 mFileUrl = appendAlbumParams(json.optString("file_url"), false).toString();
		 mCommentCnt = json.optInt("comment_cnt");
		 if(albumId == 0){
			 mAlbumId = json.optLong("album_id");
		 } else{
			 mAlbumId = albumId;
		 }
		
		 mImageType = json.optInt("image_type");
		 String photo_http_file_url = "";
		 if(!StringUtils.isEmpty(mFileUrl)){
			try {
				// 获取域名信息，用于拼凑缩略图
				photo_http_file_url = mFileUrl.substring(0,mFileUrl.indexOf("/", 7));
			} catch (StringIndexOutOfBoundsException e) {
				Log.e("Image",
						"parseImageInfoJson index out of bounds exception");
				e.printStackTrace();
			}
		 }
         
         mUrl160 = createImageThumbUrl(photo_http_file_url, 160);
 		 mUrl480 = createImageThumbUrl(photo_http_file_url, 480);
 		
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :setGroupExistAlbumJsonObject
	 * @brief	处理群相册中已存在的图片
	 * @see
	 * @since    Ver 1.1
	 * @param  @param json
	 * @param  @param gid
	 * @return void
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-11上午10:24:28
	 */
	public void setGroupExistAlbumJsonObject(JSONObject json,long gid){
		mGid = gid;
		String url ;
		if(json.has("pic_url")){
			url = json.optString("pic_url") + "&type=";
		}
		else{
			url = "http://api.pp.99.com/gthumb?app=fjoap&pic_id="+json.optString("pic_id")+"&type=";
		}
		String url_160 = getGPhotoURL(mGid+"", url + "160", "160");
		String url_480 = getGPhotoURL(mGid+"", url + "480", "480");
		setGroupAlbumJsonObject(json, url_160, url_480);
	}
	
	/**
	 * 
	 * @n<b>函数名称</b>     :getGPhotoURL
	 * @brief	缩略图url (从Albumcom搬过来的，不知道规则为什么这么复杂)
	 * @see
	 * @since    Ver 1.1
	 * @param  @param gid
	 * @param  @param url
	 * @param  @param type
	 * @param  @return
	 * @return String
	 * @<b>作者</b>          :  huangszh
	 * @<b>创建时间</b>      :  2014-3-11上午10:25:10
	 */
	public static String getGPhotoURL(String gid, String url, String type){
		String gUrl = "";
		if(null == url || "".compareTo(url) == 0){
			return gUrl;
		}
		if(url.contains("pic_id")){
			String app =  Configuration.ALBUMAPPNAME;
	 		String str = null;
	 		String pid = null;
	 		//获得加密的pic
			try {
				if (url.contains("pic_id")) {
					str = url.substring(url.indexOf("pic_id"));
					if (str.contains("&")) {
						pid = str.substring(str.indexOf("=") + 1,
								str.indexOf("&"));
					} else {
						pid = str.substring(str.indexOf("=") + 1);
					}
				}
				if (url.contains("type")) {
					str = url.substring(url.indexOf("type"));
					if (str.contains("&")) {
						url = url.substring(0, url.indexOf("type"))
								+ str.substring(str.indexOf("&") + 1)
								+ "&type=" + type;
					} else {
						url = url.substring(0, url.indexOf("type")) + "type="
								+ type;
					}
				} else {
					str += "&type=" + type;
				}
				String ckcmd5 = MD5ArithmeticUtils.getMd5(app + gid + pid + type
						+ "841aad25dc9ea84f5244199a6a2a8288");
				StringBuilder sb = new StringBuilder();
				sb.append(url);
				sb.append("&gid=").append(gid);
				sb.append("&checkcode=").append(ckcmd5);

				if (ApplicationVariable.INSTANCE.getCurrentUserInfo() != null) {
					sb.append("&sid=").append(
							ApplicationVariable.INSTANCE.getCurrentUserInfo()
									.getSessionId());
					sb.append("&unitid=").append(
							ApplicationVariable.INSTANCE.getCurrentUserInfo()
									.getOapUnitId());
				}
				sb.append("&" + Image.GROUPAPITYPE);
				sb.append("&" + Image.APPCODE);
				gUrl = sb.toString();
			} catch (StringIndexOutOfBoundsException e) {
				Log.e("getGPhotoURL", "getGPhotoURL index out of bounds");
				e.printStackTrace();
			}
		}
 		return gUrl;
	}
	
	/**
	 * 拼接图片缩略图url
	 * 
	 * <br>Created 2014-9-3 下午2:32:00
	 * @param domain  域名
	 * @param type   缩略图大小 80/160/320/780/480/1024/1600 
	 * @return String
	 * @author       huangszh
	 */
	private String createImageThumbUrl(String domain,int type){
		
		StringBuilder url = new StringBuilder();
		url.append(domain).append("/thumb/").append(mImageId).
		append("_").append(type).append(".jpg?NullPic=1");
		
		return appendAlbumParams(url,false).toString();
	}
	
	/**
	 * 相册服务端请求都要加上app,apptype,appcode,unitid
	 * 
	 * <br>Created 2014-9-5 下午2:36:48
	 * @param url   原始url 
	 * @param appendSid true的话还要往url加上sid;false不加
	 * @return StringBuilder
	 * @author       huangszh
	 */
	public static StringBuilder appendAlbumParams(String url,boolean appendSid){
		
		if(url == null){
			return null;
		}
		return appendAlbumParams(new StringBuilder(url), appendSid);
		
	}
	
	/**
	 * 相册服务端请求都要加上app,apptype,appcode,unitid
	 * 
	 * <br>Created 2014-9-5 下午2:36:48
	 * @param url   原始url 
	 * @param appendSid true的话还要往url加上sid;false不加
	 * @return StringBuilder
	 * @author       huangszh
	 */
	public static StringBuilder appendAlbumParams(StringBuilder url,boolean appendSid){
		if(url == null){
			return null;
		}
		
		if(url.toString().contains("?")){
			url.append("&app=");	
		} else{
			url.append("?app=");	
		}
		url.append(Configuration.ALBUMAPPNAME);
		url.append("&"+Image.UNITAPITYPE);
		url.append("&"+Image.APPCODE);
		
		if(ApplicationVariable.INSTANCE.getCurrentUserInfo() != null){			
			url.append("&unitid=").append(ApplicationVariable.INSTANCE.getCurrentUserInfo().getOapUnitId());
			if(appendSid){
				url.append("&sid=").append(ApplicationVariable.INSTANCE.getLocalSid());
			}
		}
		
		return url;
	}
	
	/**
	 * 为url加上sid
	 * 
	 * <br>Created 2014-9-5 下午1:52:27
	 * @param url  原始url
	 * @return String
	 * @author       huangszh
	 */
	public static String appendSid(String url){
		if(url == null){
			return null;
		}
		StringBuilder resultUrl = new StringBuilder();
		resultUrl.append(url);
		if(resultUrl.toString().contains("?")){
			resultUrl.append("&");
		} else{
			resultUrl.append("?");
		}
		return resultUrl.append("sid=").append(ApplicationVariable.INSTANCE.getLocalSid()).toString();
	}
	
 	public void setGroupAlbumJsonObject(JSONObject json, String url_160, String url_480){
 		mImageId = JSONUtils.getLong(json, "pic_id");
 		mCreateTime = JSONUtils.getString(json, "create_time");
 		mUpdateTime = mCreateTime;
 		url_160 = url_160 + "?NullPic=1&app="+Configuration.ALBUMAPPNAME + "&"
				+ GROUPAPITYPE + "&" + APPCODE 
				+ "&unitid=" + ApplicationVariable.INSTANCE.getCurrentUserInfo().getOapUnitId();
 		if(null == url_480){
 			url_480 = url_160;
 		}else{
 			url_480 = url_480 + "?NullPic=1&app="+Configuration.ALBUMAPPNAME + "&"
					+ GROUPAPITYPE + "&" + APPCODE 
					+ "&unitid=" + ApplicationVariable.INSTANCE.getCurrentUserInfo().getOapUnitId();
 		}
 		mNavCnt = JSONUtils.getInt(json, "nav_cnt"); 
 		mCreateUid = JSONUtils.getLong(json, "creator_uid"); 
 		mCommentCnt = JSONUtils.getInt(json, "comment_cnt");
		mIsMyPraise = JSONUtils.getInt(json, "liked");
		mPraiseCnt = JSONUtils.getInt(json, "likedcnt");
 	}
 	
 	/**
 	 * 获取图片原始大小url,未拼接sid
 	 * 
 	 * <br>Created 2014-9-5 下午2:02:58
 	 * @return String
 	 * @author       huangszh
 	 */
	public String getFileUrl() {
		return mFileUrl;
	}

	public void setFileUrl(String fileUrl) {
		this.mFileUrl = fileUrl;
	}
 	
	public String getFileName(){

		return MD5ArithmeticUtils.getMd5(mUrl160);
	}
 	
	/**
 	 * 获取图片160大小url,未拼接sid
 	 * 
 	 * <br>Created 2014-9-5 下午2:02:58
 	 * @return String
 	 * @author       huangszh
 	 */
	public String getUrl_160() {
		return mUrl160;
	}

	/**
 	 * 获取图片480大小url,未拼接sid
 	 * 
 	 * <br>Created 2014-9-5 下午2:02:58
 	 * @return String
 	 * @author       huangszh
 	 */
	public String getUrl_480() {
		return mUrl480;
	}

	public int getNav_cnt() {
		return mNavCnt;
	}

	public void setNav_cnt(int navCnt) {
		mNavCnt = navCnt;
	}

	public void setUrl_160(String url_160) {
		mUrl160 = url_160;
	}

	public void setUrl_480(String url_480) {
		mUrl480 = url_480;
	}

	public String getFile_dna() {
		return mFileMd5;
	}

	public void setFile_dna(String file_dna) {
		mFileMd5 = file_dna;
	}

	public long getImageId() {
		return mImageId;
	}

	public void setImageId(long imageId) {
		mImageId = imageId;
	}
	
	public void setCreateTime(String ct){
		mCreateTime = ct;
	}
	
	public void setUpdateTime(String ut){
		mUpdateTime = ut;
	}
	
	public void setGid(long id){
		mGid = id;
	}
	
	public String getCreateTime(){
		return mCreateTime;
	}

	public String getUpdateTime(){
		return mUpdateTime;
	}
	
	public long getGid(){
		return mGid;
	}
	
	public long getCreateUid(){
		return mCreateUid;
	}
	
	public void setCreateUid(long id){
		mCreateUid = id;
	}
	
	public int getCommentcnt() {
		return mCommentCnt;
	}

	public void setCommentcnt(int commentcnt) {
		mCommentCnt = commentcnt;
	}

	public int getPraisecnt() {
		return mPraiseCnt;
	}

	public void setPraisecnt(int praisecnt) {
		mPraiseCnt = praisecnt;
	}

	public int isMyPraise() {
		return mIsMyPraise;
	}

	public void setMyPraise(int isMyPraise) {
		mIsMyPraise = isMyPraise;
	}
	
	public long getAlbumId() {
		return mAlbumId;
	}

	public void setAlbumId(long albumId) {
		mAlbumId = albumId;
	}

	public int getImageType() {
		return mImageType;
	}

	public void setImageType(int imageType) {
		mImageType = imageType;
	}
	
	@Override
	public String toString() {
		return "Image [uid=" + mCreateUid + ", url_160=" + mUrl160 + ", url_480=" + mUrl480
				+ ", nav_cnt=" + mNavCnt + ", imageId=" + mImageId + ", file_url="
				+ mFileUrl+" file_dna="+mFileMd5 + "]";
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeLong(mCreateUid);
		dest.writeInt(mNavCnt);
		dest.writeString(mUrl160);
		dest.writeString(mUrl480);	
		dest.writeString(mFileMd5);	
		dest.writeLong(mImageId);
		dest.writeString(mCreateTime);
		dest.writeString(mUpdateTime);
		dest.writeLong(mGid);
		dest.writeLong(mCreateUid);
		dest.writeString(mFileUrl);
		dest.writeInt(mCommentCnt);
		dest.writeInt(mPraiseCnt);
		dest.writeInt(mIsMyPraise);
		dest.writeLong(mAlbumId);
		dest.writeInt(mImageType);
	}

	public static final Parcelable.Creator<Image> CREATOR = new Parcelable.Creator<Image>() {

		@Override
		public Image createFromParcel(Parcel source) {
			Image p = new Image();
			p.mCreateUid = source.readLong();
			p.mNavCnt= source.readInt();
			p.mUrl160 = source.readString();
			p.mUrl480 = source.readString();
			p.mFileMd5= source.readString();
			p.mImageId=source.readLong();
			p.mCreateTime=source.readString();
			p.mUpdateTime=source.readString();
			p.mGid=source.readLong();
			p.mCreateUid=source.readLong();			
			p.mFileUrl = source.readString();
			p.mCommentCnt = source.readInt();
			p.mPraiseCnt = source.readInt();
			p.mIsMyPraise = source.readInt();
			p.mAlbumId = source.readLong();
			p.mImageType = source.readInt();
			return p;
		}

		@Override
		public Image[] newArray(int size) {
			return null;
		}
	};


  
}
